package com.atguigu.tingshu.order.task;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Author  王东剑
 * Created on 23/6/2024 PM1:48
 * Package_name com.atguigu.tingshu.order.task
 * Description
 */
@Component
@EnableScheduling
@Slf4j
public class OrderScheduleTask {
	@Autowired
	private RedissonClient redissonClient;
	@Autowired
	private OrderInfoService orderInfoService;
	@Scheduled(cron = "* * * * * ?")
	public void orderCanselTask(){
		RBlockingDeque<String> rBlockingDeque = redissonClient.getBlockingDeque(RedisConstant.QUEUE_ORDER_CANCEL);
		RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(rBlockingDeque);
		String orderNo = delayedQueue.poll();
		if(!StringUtils.isEmpty(orderNo)){
			orderInfoService.update(new LambdaUpdateWrapper<OrderInfo>()
					.set(OrderInfo::getOrderStatus, SystemConstant.ORDER_STATUS_CANCEL)
					.eq(OrderInfo::getOrderNo,orderNo)
					.eq(OrderInfo::getOrderStatus,SystemConstant.ORDER_STATUS_UNPAID));
		}
	}
}
